package service.impl.v2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.AddressVOMapper;
import dao.v2.CityVOMapper;
import dao.v2.GoodTempVOMapper;
import model.AddressVO;
import model.CityVO;
import model.CompanyVO;
import model.GoodItemVO;
import model.GoodStandardVO;
import model.GoodTempItemVO;
import model.GoodTempVO;
import model.GoodVO;
import model.StandardVO;
import service.v2.GoodTempService;
@Service
public class GoodTempServiceImpl implements GoodTempService {
	@Autowired
	private GoodTempVOMapper dao;
	@Autowired
	private AddressVOMapper addressDao;
	@Autowired
	private CityVOMapper cityDao;
	
	//国际版运费算法
	public double calculateFeightEn(String[] standards, String[] numes, String address_id) {
		//根据地址获取城市
		AddressVO addressVO = addressDao.getAddressByIdEn(address_id);
		String city_id = "";
		if(addressVO.getCity_id() != null){
			city_id = addressVO.getCity_id();
			return getFreightByCityEn(standards, numes, city_id);
		}
		if(addressVO.getProvince_id() != null){
			city_id = addressVO.getProvince_id();
			return getFreightByCityEn(standards, numes, city_id);
		}
		return getFreightByCityEn(standards, numes, addressVO.getCountry_id()	);
	}
	//city_id 最低级城市ID
	public double getFreightByCityEn(String[] standards, String[] numes, String city_id) {
		CityVO city = cityDao.getCityById(city_id);
		//level 0 国家  
		//封装商品和数量
		Map<String, String> m  = new HashMap<String, String>();
		for (int i = 0; i < standards.length; i++) {
			m.put(standards[i], numes[i]);
		}

		double freight = 0;	
		List<CompanyVO> companyList = dao.getDistinctCompany(standards);
		for (CompanyVO companyVO : companyList) {
			//打包同一运费模版商品
			Map<String,List<GoodStandardVO>> map = new HashMap<String,List<GoodStandardVO>>();
			List<GoodStandardVO> standardList = dao.getStandardListByCompanyAndStandard(companyVO.getCompany_id(),standards);
			for (GoodStandardVO standardVO : standardList) {
				List<GoodStandardVO> voList =map.get(standardVO.getGood_temp_id());
				if(voList==null){
					voList = new ArrayList<GoodStandardVO>();
					voList.add(standardVO);
					map.put(standardVO.getGood_temp_id(), voList);
				}else{
					voList.add(standardVO);
				}
			}
			
			//计算运费
			double companyFreight = 0;
			for (String temp_id : map.keySet()) {
				GoodTempVO tempVO = dao.getTempEnById(temp_id);
				
				if("0".equals(city.getLevel())){
					GoodTempVO itemVO = dao.getTempCountryById(temp_id, city.getId());
					if(itemVO != null){
						tempVO.setDefault_fee(itemVO.getDefault_fee());
						tempVO.setDefault_num(itemVO.getDefault_num());
						tempVO.setAdd_fee(itemVO.getAdd_fee());
						tempVO.setAdd_num(itemVO.getAdd_num());
					}
				}else{
					GoodTempItemVO itemVO = dao.getTempCityByIdAndCity(temp_id, city.getId());
					if(itemVO != null){
						tempVO.setDefault_fee(itemVO.getItem_default_fee());
						tempVO.setDefault_num(itemVO.getItem_default_num());
						tempVO.setAdd_fee(itemVO.getItem_add_fee());
						tempVO.setAdd_num(itemVO.getItem_add_num());
					}else{
						GoodTempVO iVO = new GoodTempVO();
						if("1".equals(city.getLevel())){
							iVO = dao.getTempCityByIdAndCity2(temp_id, city.getId());
						}else if ("2".equals(city.getLevel())) {
							iVO = dao.getTempCityByIdAndCity3(temp_id, city.getId());
						}
						tempVO.setDefault_fee(iVO.getDefault_fee());
						tempVO.setDefault_num(iVO.getDefault_num());
						tempVO.setAdd_fee(iVO.getAdd_fee());
						tempVO.setAdd_num(iVO.getAdd_num());
					}
				}
				
				
				List<GoodStandardVO> gsl = map.get(temp_id);
				//按重量
				if("1".equals(tempVO.getMode())){
					int totalWeight = 0;
					for (GoodStandardVO standardVO : gsl) {
						totalWeight += Integer.parseInt(standardVO.getWeight()) * Integer.parseInt(m.get(standardVO.getStandard_id())) ;
					}
					if(totalWeight <= Integer.parseInt(tempVO.getDefault_num())* 1000){
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
					}else{
						int outNum = totalWeight-Integer.parseInt(tempVO.getDefault_num())*1000;
						int num = outNum%(Integer.parseInt(tempVO.getAdd_num())*1000)==0?(outNum/((Integer.parseInt(tempVO.getAdd_num())*1000))):(outNum/((Integer.parseInt(tempVO.getAdd_num()))*1000)+1);
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
						companyFreight += num*Double.parseDouble(tempVO.getAdd_fee());
					}
					//按件数
				}else if("2".equals(tempVO.getMode())){
					int totalNum = 0;
					for (GoodStandardVO standardVO : gsl) {
						totalNum +=  Integer.parseInt(m.get(standardVO.getStandard_id()));
					}
					
					if(totalNum <= Integer.parseInt(tempVO.getDefault_num())){
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
					}else{
						int outNum = totalNum-Integer.parseInt(tempVO.getDefault_num());
						int num = outNum%(Integer.parseInt(tempVO.getAdd_num()))==0?(outNum/(Integer.parseInt(tempVO.getAdd_num()))):(outNum/(Integer.parseInt(tempVO.getAdd_num()))+1);
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
						companyFreight += num*Double.parseDouble(tempVO.getAdd_fee());
					}
				}
			}
			freight += companyFreight;
		}
		return freight;
	}


	//运费算法
	public double calculateFeight(String[] standards, String[] numes, String address_id) {
		//根据地址获取城市
		String city_id = dao.getCityByAddress(address_id);
		return getFreightByCity(standards, numes, city_id);
	}
	
	public double getFreightByCity(String[] standards, String[] numes, String city_id) {
		//封装商品和数量
		Map<String, String> m  = new HashMap<String, String>();
		for (int i = 0; i < standards.length; i++) {
			m.put(standards[i], numes[i]);
		}

		double freight = 0;	
		List<CompanyVO> companyList = dao.getDistinctCompany(standards);
		for (CompanyVO companyVO : companyList) {
			//打包同一运费模版商品
			Map<String,List<GoodStandardVO>> map = new HashMap<String,List<GoodStandardVO>>();
			List<GoodStandardVO> standardList = dao.getStandardListByCompanyAndStandard(companyVO.getCompany_id(),standards);
			for (GoodStandardVO standardVO : standardList) {
				List<GoodStandardVO> voList =map.get(standardVO.getGood_temp_id());
				if(voList==null){
					voList = new ArrayList<GoodStandardVO>();
					voList.add(standardVO);
					map.put(standardVO.getGood_temp_id(), voList);
				}else{
					voList.add(standardVO);
				}
			}
			//计算运费
			double companyFreight = 0;
			for (String temp_id : map.keySet()) {
				GoodTempVO tempVO = dao.getTempById(temp_id);
				GoodTempItemVO itemVO = dao.getTempItem(temp_id, city_id);
				if(itemVO != null){
					tempVO.setDefault_fee(itemVO.getItem_default_fee());
					tempVO.setDefault_num(itemVO.getItem_default_num());
					tempVO.setAdd_fee(itemVO.getItem_add_fee());
					tempVO.setAdd_num(itemVO.getItem_add_num());
				}
				List<GoodStandardVO> gsl = map.get(temp_id);
				//按重量
				if("1".equals(tempVO.getMode())){
					int totalWeight = 0;
					for (GoodStandardVO standardVO : gsl) {
						totalWeight += Integer.parseInt(standardVO.getWeight()) * Integer.parseInt(m.get(standardVO.getStandard_id())) ;
					}
					if(totalWeight <= Integer.parseInt(tempVO.getDefault_num())* 1000){
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
					}else{
						int outNum = totalWeight-Integer.parseInt(tempVO.getDefault_num())*1000;
						int num = outNum%(Integer.parseInt(tempVO.getAdd_num())*1000)==0?(outNum/((Integer.parseInt(tempVO.getAdd_num())*1000))):(outNum/((Integer.parseInt(tempVO.getAdd_num()))*1000)+1);
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
						companyFreight += num*Double.parseDouble(tempVO.getAdd_fee());
					}
					//按件数
				}else if("2".equals(tempVO.getMode())){
					int totalNum = 0;
					for (GoodStandardVO standardVO : gsl) {
						totalNum +=  Integer.parseInt(m.get(standardVO.getStandard_id()));
					}
					
					if(totalNum <= Integer.parseInt(tempVO.getDefault_num())){
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
					}else{
						int outNum = totalNum-Integer.parseInt(tempVO.getDefault_num());
						int num = outNum%(Integer.parseInt(tempVO.getAdd_num()))==0?(outNum/(Integer.parseInt(tempVO.getAdd_num()))):(outNum/(Integer.parseInt(tempVO.getAdd_num()))+1);
						companyFreight += Double.parseDouble(tempVO.getDefault_fee());
						companyFreight += num*Double.parseDouble(tempVO.getAdd_fee());
					}
				}
			}
			freight += companyFreight;
		}
		return freight;
	}

	public List<StandardVO> getStandardType() {
		return dao.getStandardType();
	}

	public List<StandardVO> getStandardChild(String type_id) {
		return dao.getStandardChild(type_id);
	}
	
	//@Cacheable(value="GoodTempServiceImpl_check",key="#root.targetClass+#root.methodName+#a")
	public List<GoodVO> check(String a) {
		return dao.check();
	}

	public void testaop() {
		
	}
		
}
